package com.fanbai.score.business.fragemnt;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.business.beans.ShopInfo_Bean;
import com.fanbai.score.business.http.Busin_Goods_RequestNet;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.dialog.MyLayoutDialog;
import com.fanbai.score.common.dialog.OnMyAlertDialogClick;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.bean.ShopClass_Bean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/6/17.
 */

public class ManageShop_Fragment extends BaseFragment{

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.businessFragment_addShop)
    Button addShopBut;
    @OnClick(R.id.businessFragment_addShop)
    public void onClick(){
        MyLayoutDialog.instanceRenterAddUser(ctx, new OnMyAlertDialogClick() {
            @Override
            public void onMyAlertDialogClick(boolean is, String... str) {
                addshopinfo(str[0], str[1], str[2], str[3]);
            }
        }).show();
    }
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static ManageShop_Fragment newInstance(int sectionNumber) {
        ManageShop_Fragment fragment = new ManageShop_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ManageShop_Fragment() {
    }
    @Override
    protected int getLayoutID() {
        return R.layout.business_fragment_manage_shop;
    }

    @Override
    public String getTitleName() {
        return null;
    }

    @Override
    public void iniTitleLeft(ImageView tv) {

    }

    @Override
    public void iniTitleRight(TextView tv) {

    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {

        getshopinfos();

    }

    private void setData(List<ShopInfo_Bean> beans){
//        List<String> dataList = new ArrayList<>();
//        dataList.add("htsec01.png");
//        dataList.add("htsec01.png");
//        dataList.add("htsec01.png");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        CommonAdapter adapter = new CommonAdapter<ShopInfo_Bean>(ctx, R.layout.business_item_managefragment_shop,
                beans){
            @Override
            protected void convert(final ViewHolder holder, final ShopInfo_Bean bean, int position) {
//                holder.setText(R.id.itemTextView, bean.getClsname());
                final LinearLayout infoLayout = holder.getView(R.id.busionessItemManFragment_shopInfoLayout);
                holder.setOnClickListener(R.id.busionessItemManFragment_shopShowInfoImg, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(infoLayout.getVisibility() == View.VISIBLE){
                            holder.setImageResource(R.id.busionessItemManFragment_shopShowInfoImg, R.mipmap.icon_up);
                            infoLayout.setVisibility(View.GONE);
                        }else{
                            holder.setImageResource(R.id.busionessItemManFragment_shopShowInfoImg, R.mipmap.icon_down);
                            infoLayout.setVisibility(View.VISIBLE);
                        }

                    }
                });
                holder.setImageResource(R.id.businessItemFragmentShop_img, R.mipmap.icon_img_defeated);
                holder.setText(R.id.businessItemFragmentShop_shopName, bean.getSname());
                holder.setText(R.id.businessItemFragmentShop_shopAddress, bean.getSdetailaddress());
                holder.setText(R.id.businessItemFragmentShop_shopNewName, bean.getSname());
                holder.setText(R.id.businessItemFragmentShop_shopNewAddress, bean.getSdetailaddress());
                holder.setOnClickListener(R.id.businessItemFragmentShop_submitBut, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String shopName = ((EditText)holder.getView(R.id.businessItemFragmentShop_shopNewName)).getText().toString();
                        String shopAddress = ((EditText)holder.getView(R.id.businessItemFragmentShop_shopNewAddress)).getText().toString();
                        String userName = ((EditText)holder.getView(R.id.businessItemFragmentShop_userNewName)).getText().toString();
                        String userMobile = ((EditText)holder.getView(R.id.businessItemFragmentShop_userNewMobile)).getText().toString();
                        if(TextUtils.isEmpty(shopName)){
                            ToastUtils.WarnImageToast(ctx, "店铺名称不可为空");
                            return ;
                        }
                        if(TextUtils.isEmpty(shopAddress)){
                            ToastUtils.WarnImageToast(ctx, "店铺地址不可为空");
                            return ;
                        }
                        if(TextUtils.isEmpty(userName)){
                            ToastUtils.WarnImageToast(ctx, "联系人姓名不可为空");
                            return ;
                        }
                        if(TextUtils.isEmpty(userMobile)){
                            ToastUtils.WarnImageToast(ctx, "联系人电话不可为空");
                            return ;
                        }
                        editshopinfo(bean.getSid(), shopName, shopAddress, userName, userMobile);
                    }
                });
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    private void getshopinfos(){
        ctx.showDialog();
        Busin_Goods_RequestNet.getshopinfos(ctx, new CallBack<List<ShopInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<ShopInfo_Bean> shopInfo_been) {
                ctx.hideDialog();
                setData(shopInfo_been);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void editshopinfo(String sid, String sname, String sdetailaddress, String slinkmanname, String slinkmanmobile){
        ctx.showDialog();
        Busin_Goods_RequestNet.editshopinfo(ctx, sid, sname, sdetailaddress, slinkmanname, slinkmanmobile, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ctx.hideDialog();
                initData();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void addshopinfo(String shopName, String shopAdd, String userName, String userMobile){
        ctx.showDialog();
        Busin_Goods_RequestNet.addshopinfo(ctx, shopName, shopAdd, userName, userMobile, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ctx.hideDialog();
                ToastUtils.RightImageToast(ctx, "添加成功"+msg);
                initData();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
